home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / unixlib.lha / unix / src / protection.c < prev    next >
C/C++ Source or Header  |  1995-09-05  |  2KB  |  91 lines

  1.  
  2. #include "amiga.h"
  3. #include <sys/stat.h>
  4.  
  5. int use_amiga_flags;
  6.  
  7. int _make_protection(int mode)
  8. {
  9.     int amode;
  10.  
  11.     if (use_amiga_flags)
  12.     return mode;
  13.  
  14.     /* We always turn archive off */
  15.     amode = 0;
  16.  
  17.     /* Read: if any unix read */
  18.     if (mode & (S_IRUSR | S_IRGRP | S_IROTH))
  19.     amode |= FIBF_READ;
  20.  
  21.     /* Write: if user write or group write
  22.        Delete: if user write or world write */
  23.     if (mode & S_IWUSR)
  24.     amode |= FIBF_WRITE | FIBF_DELETE;
  25.     if (mode & S_IWGRP)
  26.     amode |= FIBF_WRITE;
  27.     if (mode & S_IWOTH)
  28.     amode |= FIBF_DELETE;
  29.  
  30.     /* Execute: if group execute or user execute and not world execute
  31.        Script: if world execute or user execute ant not group execute */
  32.     if (mode & S_IXGRP)
  33.     amode |= FIBF_EXECUTE;
  34.     if (mode & S_IXOTH)
  35.     amode |= FIBF_SCRIPT;
  36.     if ((mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == S_IXUSR)
  37.     amode |= FIBF_EXECUTE | FIBF_SCRIPT;
  38.  
  39.     /* Pure: if sticky */
  40.     if (mode & S_ISVTX)
  41.     amode |= FIBF_PURE;
  42.  
  43.     /* Make correct bits active 0 */
  44.     amode ^= FIBF_READ | FIBF_WRITE | FIBF_EXECUTE | FIBF_DELETE;
  45.     return amode;
  46. }
  47.  
  48. int _make_mode(int protection)
  49. {
  50.     int mode;
  51.  
  52.     if (use_amiga_flags)
  53.     return protection & ~S_IFMT;
  54.  
  55.     mode = 0;
  56.     /* make all bits active 1 */
  57.     protection ^= FIBF_READ | FIBF_WRITE | FIBF_EXECUTE | FIBF_DELETE;
  58.  
  59.     /* Read user, group, world if amiga read */
  60.     if (protection & FIBF_READ)
  61.     mode |= S_IRUSR | S_IRGRP | S_IROTH;
  62.  
  63.     /* Write:
  64.        user if amiga write & delete
  65.        group if amiga write
  66.        other if amiga delete */
  67.     if ((protection & (FIBF_WRITE | FIBF_DELETE)) == (FIBF_WRITE | FIBF_DELETE))
  68.     mode |= S_IWUSR;
  69.     if (protection & FIBF_WRITE)
  70.     mode |= S_IWGRP;
  71.     if (protection & FIBF_DELETE)
  72.     mode |= S_IWOTH;
  73.  
  74.     /* Execute:
  75.        user if amiga execute or script
  76.        group if amiga execute
  77.        world if amiga script */
  78.     if (protection & (FIBF_EXECUTE | FIBF_SCRIPT))
  79.     mode |= S_IXUSR;
  80.     if (protection & FIBF_EXECUTE)
  81.     mode |= S_IXGRP;
  82.     if (protection & FIBF_SCRIPT)
  83.     mode |= S_IXOTH;
  84.  
  85.     /* Sticky: if pure */
  86.     if (protection & FIBF_PURE)
  87.     mode |= S_ISVTX;
  88.  
  89.     return mode;
  90. }
  91.